package com.dikeleakagedetection.myapplication.utils

import com.dikeleakagedetection.myapplication.logic.dao.Dot
import java.lang.Exception

object MsgUtils {
    fun parseMsg(str: String, tabletId: Long): Dot? {
        val sList: List<String> = str.split("/")
        var dot: Dot? = null
        try {
            val rollX = sList[0].split(":")[1].toDouble()
            val pitchY = sList[1].split(":")[1].toDouble()
            val yawZ = sList[2].split(":")[1].toDouble()
            val tempLongitude = sList[3].split(":")[1]
            val longitude = tempLongitude.subSequence(0, tempLongitude.length).toString().toDouble()
            val tempLatitude = sList[4].split(":")[1]
            val latitude = tempLatitude.subSequence(0, tempLatitude.length).toString().toDouble()
            val tempAltitude = sList[5].split(":")[1]
            val altitude = tempAltitude.subSequence(0, tempAltitude.length).toString().toDouble()
            val vX = sList[6].split(":")[1]
            val vY = sList[7].split(":")[1]
            val vZ = sList[8].split(":")[1]
            dot = Dot(
                rollX,
                pitchY,
                yawZ,
                longitude,
                latitude,
                altitude,
                vX,
                vY,
                vZ,
                tabletId
            )
        } catch (e: Exception) {
            "数据格式错误".showToast()
            e.printStackTrace()
        }
        return dot
    }

    fun parseMsg(dot: Dot, str: String): Boolean {
        val sList: List<String> = str.split(":")
        var res = false
        try {
            res = when (sList[0]) {
                "Roll(X)" -> {
                    dot.attitudeX = sList[1].toDouble()
                    false
                }
                "Pitch(Y)" -> {
                    dot.attitudeY = sList[1].toDouble()
                    false
                }
                "Yaw(Z)" -> {
                    dot.attitudeZ = sList[1].toDouble()
                    false
                }
                "Longitude" -> {
                    dot.locationLng = sList[1].split(",")[0]
                    false
                }
                "Latitude" -> {
                    dot.locationLat = sList[1].split(",")[0]
                    false
                }
                "Altitude" -> {
                    dot.locationAlt = sList[1].split("m")[0]
                    false
                }
                "Vx" -> {
                    dot.magneticX = sList[1].toDouble()
                    false
                }
                "Vy" -> {
                    dot.magneticY = sList[1].toDouble()
                    false
                }
                "Vz" -> {
                    dot.magneticZ = sList[1].toDouble()
                    true
                }
                else -> false
            }
        }catch (e:Exception){
            e.printStackTrace()
        }

        return res
    }
}